package com.crayfish.quduo.ui.fragment;

import android.content.Intent;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.crayfish.quduo.R;
import com.crayfish.quduo.base.BaseMvpActivity;
import com.crayfish.quduo.base.BaseMvpFragment;
import com.crayfish.quduo.http.RetrofitHelper;
import com.crayfish.quduo.model.UserBean;
import com.crayfish.quduo.presenter.MePresenter;
import com.crayfish.quduo.presenter.contract.MeContract;
import com.crayfish.quduo.ui.activity.HistoryActivity;
import com.crayfish.quduo.ui.activity.LoginActivity;
import com.crayfish.quduo.ui.activity.MyNewsActivity;
import com.crayfish.quduo.ui.activity.PersonalActivity;
import com.crayfish.quduo.ui.activity.SettingActivity;
import com.crayfish.quduo.utils.ImageUtil;
import com.crayfish.quduo.utils.ToastUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by crayfish on 2016/12/29.
 */
public class MeFragment extends BaseMvpFragment<MePresenter> implements MeContract.View{

    @BindView(R.id.txt_setting)
    TextView txt_setting;
    @BindView(R.id.txt_news)
    TextView txt_news;
    @BindView(R.id.ll_user)
    LinearLayout ll_user;
    @BindView(R.id.iv_head_portrait)
    ImageView iv_head_portrait;
    @BindView(R.id.txt_username)
    TextView txt_username;
    @BindView(R.id.txt_wallet)
    TextView txt_wallet;

    private boolean isLogin = false;
    private boolean isOne = true;

    @Override
    protected int getLayoutId() {
        return R.layout.fragemnt_me;
    }

    @Override
    protected void initEventAndData() {
//        mPresenter.getUserBean(getActivity());
    }

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.getUserBean(getActivity());
    }

    @Override
    protected MePresenter createPresenter() {
        return new MePresenter(new RetrofitHelper());
    }

    @Override
    public void showError(String msg) {
        ToastUtil.show(msg);
    }

    @Override
    public void showUser(UserBean userBean) {
        if(userBean != null){
            isLogin = true;
            ImageUtil.imageBitmapLoad(getActivity(),userBean.getPortrait(),iv_head_portrait);
            txt_username.setText(userBean.getUsername());
        }
    }

    @Override
    public void gotoLogin() {
        iv_head_portrait.setImageResource(R.drawable.qq);
        txt_username.setText("");
        isLogin = false;
        if(isOne) {
            isOne = false;
            startMyActivity(LoginActivity.class);
        }
    }

    @OnClick(R.id.txt_setting)
    void settingClick(){
        startMyActivity(SettingActivity.class);
    }

    @OnClick(R.id.txt_record)
    void recordClick(){
        startMyActivity(HistoryActivity.class);
    }

    @OnClick(R.id.txt_wallet)
    void walletClick(){
        ToastUtil.show("暂未开放");
    }

    @OnClick(R.id.txt_vip)
    void vipClick(){
        ToastUtil.show("暂未开放");
    }

    @OnClick(R.id.txt_news)
    void newsClick(){
        startMyActivity(MyNewsActivity.class);
    }

    @OnClick(R.id.ll_user)
    void loginClick(){
        if(!isLogin){
            startMyActivity(LoginActivity.class);
        }else{
            startMyActivity(PersonalActivity.class);
        }
    }
}
